package me.minecraft.plugin.pokemontip;

import com.cobblemon.mod.common.api.Priority;
import com.cobblemon.mod.common.api.events.CobblemonEvents;
import com.cobblemon.mod.common.api.reactive.Observable;
import com.cobblemon.mod.common.pokeball.PokeBall;
import com.cobblemon.mod.common.pokemon.Species;
import com.cobblemon.mod.common.util.MiscUtilsKt;
import lg.minecraft.plugin.cobblemoncore.util.CobblemonEventHandler;
import lombok.Getter;
import me.minecraft.plugin.pokemontip.command.MainCommand;
import me.minecraft.plugin.pokemontip.config.MainConfig;
import me.minecraft.plugin.pokemontip.listener.CobblemonEvent;
import net.minecraft.class_3222;
import net.minecraft.server.level.EntityPlayer;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

public final class PokemonTip extends JavaPlugin {

    @Getter private static PokemonTip instance;
    @Getter private static MainConfig mainConfig;

    @Override
    public void onEnable() {
        instance = this;
        mainConfig = new MainConfig(this);
        registerCobblemonEvent(CobblemonEvents.POKEMON_CAPTURED, Priority.LOWEST, CobblemonEvent::onPokemonCaptured);
        registerCobblemonEvent(CobblemonEvents.POKEMON_ENTITY_SPAWN, Priority.LOWEST, CobblemonEvent::onPokemonEntitySpawn);
        this.registerCommand("PokemonTip", new MainCommand());
    }

    private void registerCommand(String name, CommandExecutor executor) {
        PluginCommand command = getCommand(name);
        if (command == null) {
            getLogger().warning("无法注册命令：" + name);
            return;
        }
        command.setExecutor(executor);
    }

    public static Player toPlayer(@NotNull class_3222 player) {
        return EntityPlayer.class.cast(player).getBukkitEntity().getPlayer();
    }

    public static String getPokemonName(Species species) {
        return species.getTranslatedName().getString();
    }

    public static String getPokeBallName(PokeBall pokeBall) {
        return MiscUtilsKt.asTranslated("item.cobblemon." + pokeBall.getName().method_12832()).getString();
    }

    private <T> void registerCobblemonEvent(Observable<T> observable, Priority priority, Consumer<T> handler) {
        CobblemonEventHandler.registerCobblemonEvent(this, observable, priority, handler);
    }

}
